package com.v.im.businessservice.service.impl;

import com.v.im.businessapi.dto.resultDTO.ImUserResultDTO;
import com.v.im.businessservice.bean.ImChatGroupUserEntity;
import com.v.im.businessservice.bean.ImGroupEntity;
import com.v.im.businessservice.bean.ImUserEntity;
import com.v.im.businessservice.bean.ImUserFriendEntity;
import com.v.im.businessservice.service.base.BaseService;
import com.v.im.businessservice.service.iface.IImUserService;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Timestamp;
import java.util.*;
import java.util.stream.Collectors;

@Component
public class ImUserServiceImpl extends BaseService implements IImUserService {

    @Value("${v.im.admin.id}")
    private String adminId;

    @Value("${v.im.default.chat.id}")
    private String defaultChatId;

    @Override
    public ImUserResultDTO getImUserrInfoByUserName(String userName) {
        ImUserResultDTO resultDTO = new ImUserResultDTO();
        ImUserEntity imUserEntity = imUserDao.findByName(userName);
        if (imUserEntity != null) {
            BeanUtils.copyProperties(imUserEntity, resultDTO);
        }
        return resultDTO;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean saveUserInfo(ImUserEntity imUser) {
        Boolean flag = true;
        try {
         imUser.setId(UUID.randomUUID().toString().substring(0,20));
        imUserDao.save(imUser);
        //添加默认用户分组 我的好友
        ImGroupEntity imGroup = new ImGroupEntity();
        imGroup.setId(UUID.randomUUID().toString().substring(0,20));
        imGroup.preInsert();
        imGroup.setName("我的好友");
        imGroup.setUserId(imUser.getId());
        imGroupDao.save(imGroup);

        //更新默认的用户组
        imUser.setDefaultGroupId(imGroup.getId());
        imUserDao.save(imUser);

        //保存用户好友，默认管理员
        ImUserFriendEntity imUserFriend = new ImUserFriendEntity();
        imUserFriend.preInsert();
        imUserFriend.setUserId(imUser.getId());
        imUserFriend.setFriendId(adminId);
        imUserFriend.setUserGroupId(imGroup.getId());
        //默认好友的分组
        ImUserEntity friend =imUserDao.findById(adminId);
        imUserFriend.setFriendGroupId(friend.getDefaultGroupId());
        imUserFriendDao.save(imUserFriend);

        //添加默认群
        ImChatGroupUserEntity imChatGroupUser = new ImChatGroupUserEntity();
        imChatGroupUser.setUserId(imUser.getId());
        imChatGroupUser.setChatGroupId(defaultChatId);
        imChatGroupUser.setCreateDate(new Timestamp(new Date().getTime()));
        imChatGroupUserDao.save(imChatGroupUser);
    } catch (Exception e) {
        flag = false;
        e.printStackTrace();
        throw new RuntimeException(e);
    }
        return flag;
    }


    @Override
    public List<ImUserResultDTO> getImUserResults(String userIds) {
        Set<String> userId = Arrays.stream(userIds.split(",")).collect(Collectors.toSet());
        List<ImUserEntity> imUserEntities = imUserDao.findByIdIn(userId);
        List<ImUserResultDTO> imUserResultDTOS = new ArrayList<>();
        if (CollectionUtils.isNotEmpty(imUserEntities)) {
            for (ImUserEntity imUserEntity : imUserEntities) {
                ImUserResultDTO imUserResultDTO = new ImUserResultDTO();
                BeanUtils.copyProperties(imUserEntity, imUserResultDTO);
                imUserResultDTOS.add(imUserResultDTO);
            }
        }
        return imUserResultDTOS;
    }
}
